package GUI;

import 操作.会员卡_操作;
import 实体.会员卡;
import 操作.消费记录_操作;
import 实体.消费记录;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;

public class 会员信息界面 {
    private String s1;
    private JFrame frame;
    private JPanel main;
    private JButton 售卡Button;
    private JButton 系统管理Button;
    private JButton 会员信息Button;
    private JButton 消费记录Button;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField5;
    private JButton 存款Button;
    private JButton 消费Button1;
    private JButton 挂失Button1;
    private JButton 退卡Button1;
    private JButton 解禁Button;
    private JTextField textField4;
    private JTextField textField6;
    private JTextField textField7;
    private JTextField textField8;
    private JButton 返回会员登录界面Button;

    public 会员信息界面() {

        返回会员登录界面Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

            }
        });
    }

    public 会员信息界面(String s) {
        会员卡_操作 会员卡信息=new 会员卡_操作();
        try {
            List<会员卡> list=会员卡信息.搜索(s);
            textField1.setText(list.get(0).get卡号());
            textField1.setEnabled(false);
            textField2.setText(list.get(0).get姓名());
            textField2.setEnabled(false);
            textField3.setText(list.get(0).get类型());
            textField3.setEnabled(false);
            textField4.setText(list.get(0).get金额());
            textField4.setEnabled(false);
            textField5.setText(list.get(0).get折扣());
            textField5.setEnabled(false);
            textField6.setText(list.get(0).get状态());
            textField6.setEnabled(false);
        }catch (SQLException e){
            e.printStackTrace();
        }
        挂失Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String []ss={textField3.getText(),textField4.getText(),textField5.getText(),"挂失",textField1.getText()};
                try {
                    会员卡信息.修改(ss);
                    new 会员信息界面().界面(textField1.getText());
                    frame.dispose();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        解禁Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String []ss={textField3.getText(),textField4.getText(),textField5.getText(),"正常",textField1.getText()};
                try {
                    会员卡信息.修改(ss);
                    new 会员信息界面().界面(textField1.getText());
                    frame.dispose();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        消费Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                消费记录_操作 消费记录操作=new 消费记录_操作();
                int num1=Integer.parseInt(textField4.getText());

                if (textField7.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "消费金额不能为空！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                int num2=Integer.parseInt(textField7.getText());
                if ((num1-num2)<0){
                    JOptionPane.showMessageDialog(null, "余额不足！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                else {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date d=new Date();
                    String date=sdf.format(d);
                    String num3=String.valueOf(num1-num2);
                    String[] s={textField1.getText(),textField2.getText(),date,textField7.getText(),"0",num3};
                    try {
                        List<消费记录> list=消费记录操作.增加(s);
                        if (list==null){
                            JOptionPane.showMessageDialog(null, "非营业时间系统维护！");
                        }
                        else {
                            System.out.println(num3);
                            JOptionPane.showMessageDialog(null, "消费成功！");
                            String []ss={textField3.getText(),num3,textField5.getText(),textField6.getText(),textField1.getText()};
                            会员卡信息.修改(ss);
                            new 会员信息界面().界面(textField1.getText());
                            frame.dispose();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        存款Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                消费记录_操作 消费记录操作=new 消费记录_操作();
                int num1=Integer.parseInt(textField4.getText());

                if (textField8.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "预存金额不能为空！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                else {
                    int num2=Integer.parseInt(textField8.getText());
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date d=new Date();
                    String date=sdf.format(d);
                    String num3=String.valueOf(num1+num2);
                    String[] s={textField1.getText(),textField2.getText(),date,"0",textField8.getText(),num3};
                    try {
                        List<消费记录> list=消费记录操作.增加(s);
                        JOptionPane.showMessageDialog(null, "消费成功！");
                        String []ss={textField3.getText(),num3,textField5.getText(),textField6.getText(),textField1.getText()};
                        会员卡信息.修改(ss);
                        new 会员信息界面().界面(textField1.getText());
                        frame.dispose();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        退卡Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    会员卡信息.删除(textField1.getText());
                    JOptionPane.showMessageDialog(null, "退卡成功！");
                    new 会员登录界面().界面();
                    frame.dispose();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        });
        返回会员登录界面Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                new 会员登录界面().界面();
                frame.dispose();
            }
        });
    }

    public static void main(String[] args) {
        界面("2020081321");
    }
    public static void 界面(){
        会员信息界面 会员=new 会员信息界面();
        会员.frame=new JFrame("游泳管理系统");
        会员.frame.setContentPane(会员.main);
        会员.frame.setSize(500,400);
        会员.frame.setLocation(500, 200);
        会员.frame.setVisible(true);
        会员.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void 界面(String s){
        会员信息界面 会员=new 会员信息界面(s);
        会员.frame=new JFrame("游泳管理系统");
        会员.frame.setContentPane(会员.main);
        会员.frame.setSize(500,400);
        会员.frame.setLocation(500, 200);
        会员.frame.setVisible(true);
        会员.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
